package com.twopai.twopai;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.alibaba.android.arouter.launcher.ARouter;
import com.google.gson.Gson;
import com.twopai.appbase.routerpath.ARouterManager;
import com.twopai.appbase.routerpath.RoutePath;
import com.twopai.framelibrary.http.BaseObjectBean;
import com.twopai.framelibrary.mvp.BaseStatusActivity;
import com.twopai.login.R2;
import com.twopai.twopai.contact.RunShopFragContract;
import com.twopai.framelibrary.entity.KDItemEntity;
import com.twopai.twopai.presenter.RunShopFragPresenter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class MainActivity extends BaseStatusActivity<RunShopFragPresenter> implements RunShopFragContract.View {

    @BindView(R.id.btn_Login)
    Button btnLogin;
    @BindView(R.id.cont_txt)
    TextView cont_txt;
    @BindView(R.id.toolBarCenterText)
    TextView toolBarCenterText;
    private RunShopFragPresenter presenter;
    private String TAG="MainActivity";
    private List<KDItemEntity.ListBean> kdItemEntityList=new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void iniTitle() {

    }



    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        presenter = new RunShopFragPresenter();
        presenter.attachView(this);
        toolBarCenterText.setText("主页");
    }

    @Override
    public void initData() {
        kdItemEntityList.clear();
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("token", "f35033cfd9cc4e52b3abd6d427e4037d");
        dataMap.put("user_id","8");
        dataMap.put("page", 1);
        String json = new Gson().toJson(dataMap);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);
        Log.e(TAG, ",json=" + json);
        presenter.newsList(body);
    }


    @Override
    public void newsListSuccess(BaseObjectBean<KDItemEntity> bean) {
        if (bean != null) {
            int status = bean.getStatus();
            if (status == 1) {
                KDItemEntity kdItemEntity = bean.getData();
                if (kdItemEntity != null) {
                    List<KDItemEntity.ListBean> kdItemEntityListCur = kdItemEntity.getList();
                    if (kdItemEntityListCur != null&&kdItemEntityListCur.size()>0) {
                        kdItemEntityList.addAll(kdItemEntityListCur);
                        cont_txt.setText(kdItemEntityList.toString());
                    }else {

                    }
                }else {

                }
            }else {
//                handMsg(status,bean.getMessage());

            }
        }else {

        }
    }

    @Override
    public void newsListError(Throwable e) {
        cont_txt.setText(e.getMessage());
    }


    @OnClick({R.id.btn_Login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_Login:
                if(kdItemEntityList.size()==0)return;
                KDItemEntity.ListBean listBean = kdItemEntityList.get(0);
                ARouterManager.getInstance().build(RoutePath.ROUTER_LOGIN)
                        .withString("title", listBean.getTitle())
                        .withInt("itemId",listBean.getId())
                        .navigation(this, 101);
//                ARouter.getInstance().build(RoutePath.ROUTER_LOGIN)
//                        .withString("title", listBean.getTitle())
//                        .withInt("itemId",listBean.getId())
//                        .navigation(this, 101);
                break;
        }
    }

    @Override
    protected int setToolBarId() {
        return R.id.toolBarWholeLinear;
    }
}
